// pages/integral-detail/integral-detail.js
import { getIntegralProductDetail, insertIntegralOrder } from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    detail: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      did: options.did
    })
    this._getDetail()
  },
  _getDetail() {
    getIntegralProductDetail({
      data: {
        id: this.data.did
      },
      success: res => {
        if (res.state) {
          let detail = res.data.detail === null ? '' : res.data.detail.replace(/<img/g, "<img style='max-width:100%'")
          detail = detail.replace(/section/g, "div")
          res.data.detail = detail
          this.setData({
            detail: res.data
          })
        }
      }
    })
  },
  formOrder() {
    var obj = {}
    obj.name = this.data.detail.name
    obj.sku = this.data.detail.sku[0].title
    obj.img = this.data.detail.mainimg.URL
    obj.price = this.data.detail.sku[0].price
    obj.count = 1
    obj.productId = this.data.detail.product_Id
    var pro = []
    pro.push(obj)
    this.data.order.express_fee = '暂未需要';
    this.data.order.remark = '暂无备注';
    this.data.order.order_state = "待发货";
    this.data.order.buy_openid = app.globalData.openid;
    // order.shareOpenid = shareopenid;
    this.data.order.products = pro;
    this.data.order.order_total_integral = parseFloat(obj.price) * parseFloat(obj.count);
    this.data.order.cid = 'admins';
  },
  exchange() {
    wx.showModal({
      title: '积分兑换',
      content: `您将花费${this.data.detail.sku[0].price}积分兑换${this.data.detail.name}`,
      success: re => {
        if (re.confirm) {
          this.formOrder()
          console.log(this.data.order)
          insertIntegralOrder({
            data: {
              order: JSON.stringify(this.data.order),
              payType: "1"
            },
            success: res => {
              if (res.state) {
                wx.showToast({
                  title: '兑换成功！',
                  icon: 'none'
                })
                setTimeout(() => {
                  wx.navigateTo({
                    url: '../exchange-list/exchange-list',
                  })
                }, 1500)
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'none'
                })
              }
            }
          })
        } else if (re.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})